/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007-2008 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.web.gwt.form.client.validator.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.genia.toolbox.web.gwt.basics.client.i18n.GwtI18nMessage;
import com.genia.toolbox.web.gwt.form.client.validator.Validator;
import com.genia.toolbox.web.gwt.form.client.validator.ValidatorCallback;

/**
 * a {@link Validator} that aggregates several {@link Validator}.
 */
public class AggregateValidator
    implements Validator
{

  /**
   * the {@link List} of {@link Validator} to aggregate.
   */
  private List<Validator> validators;



  /**
   * validate if the item's values is acceptable.
   * 
   * @param values
   *          the values to validate.
   * @param callback
   *          the callback that will be called once the validation is done
   * @see com.genia.toolbox.web.gwt.form.client.validator.Validator#validate(java.util.List,
   *      com.genia.toolbox.web.gwt.form.client.validator.ValidatorCallback)
   */
  public void validate(final List<String> values, final ValidatorCallback callback)
  {
    if (validators == null) {
      callback.validationResult(null);
    }
    continueValidation(values, callback, validators.iterator(), new ArrayList<GwtI18nMessage>());
  }



  /**
   * do one step of validation.
   * 
   * @param values
   *          the values to validate.
   * @param callback
   *          the callback that will be called once the validation is done
   * @param validatorsIterator
   *          the current {@link Iterator} over the {@link List} of
   *          {@link Validator} to aggregate
   * @param currentResults
   *          the current results of the validation
   */
  private void continueValidation(final List<String> values, final ValidatorCallback callback, final Iterator<Validator> validatorsIterator, final List<GwtI18nMessage> currentResults)
  {
    if (!validatorsIterator.hasNext()) {
      callback.validationResult(currentResults);
      return;
    }
    validatorsIterator.next().validate(values, new ValidatorCallback()
    {

      /**
       * validate if the item's values is acceptable.
       * 
       * @param values
       *          the values to validate.
       * @param callback
       *          the callback that will be called once the validation is done
       * @see com.genia.toolbox.web.gwt.form.client.validator.Validator#validate(java.util.List,
       *      com.genia.toolbox.web.gwt.form.client.validator.ValidatorCallback)
       */
      public void validationResult(List<GwtI18nMessage> result)
      {
        currentResults.addAll(result);
        continueValidation(values, callback, validatorsIterator, currentResults);
      }

    });
  }



  /**
   * getter for the validators property.
   * 
   * @return the validators
   */
  public List<Validator> getValidators()
  {
    return validators;
  }



  /**
   * setter for the validators property.
   * 
   * @param validators
   *          the validators to set
   */
  public void setValidators(List<Validator> validators)
  {
    this.validators = validators;
  }

}
